package io.renren.modules.generator.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.generator.entity.DeviceSiteEntity;
import io.renren.modules.generator.service.DeviceSiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 设备定位
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-08-27 15:31:18
 */
@RestController
@RequestMapping("generator/devicesite")
@Api("设备定位接口")
public class DeviceSiteController {
    @Autowired
    private DeviceSiteService deviceSiteService;

    /**
     * 列表
     */
    @RequestMapping("/all/list")
    @ApiOperation("根据设备类型获取所有点位信息")
    public R alllist(@RequestParam(value = "type",required = false,defaultValue = "") String type,
                     @RequestParam(value = "exclude",required = false,defaultValue = "false")
                     @Pattern(regexp = "true|false",message = "只允许输入true或false") String exclude){
        Map<String, List<DeviceSiteEntity>> list = deviceSiteService.selecctAllList(type,exclude);
        return R.ok().put("list", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ApiOperation("分页列表")
    public R list(@RequestParam Map<String, Object> params,@RequestBody(required = false) DeviceSiteEntity deviceSite){
        PageUtils page = deviceSiteService.queryPage(params,deviceSite);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{deviceCode}")
    @ApiOperation("根据id获取实体")
    public R info(@PathVariable("deviceCode") String deviceCode){
		DeviceSiteEntity deviceSite = deviceSiteService.getById(deviceCode);

        return R.ok().put("deviceSite", deviceSite);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ApiOperation("保存")
    public R save(@RequestBody DeviceSiteEntity deviceSite){
		deviceSiteService.save(deviceSite);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ApiOperation("更新")
    public R update(@RequestBody DeviceSiteEntity deviceSite){
		deviceSiteService.updateById(deviceSite);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ApiOperation("删除")
    public R delete(@RequestBody String[] deviceCodes){
		deviceSiteService.removeByIds(Arrays.asList(deviceCodes));

        return R.ok();
    }

}
